#!/usr/bin/env php
<?php

# A client that runs three unrelated tasks at once.

function complete($task, $result)
{
  $result['data'] = $task->data();
  $result['handle'] = $task->jobHandle();
}

$begin = gettimeofday(true);

$client = new GearmanClient();
$client->addServer('127.0.0.1', 4730);

$results = array();

$client->addTask('dmesg',
                 '20',
                 &$results['dmesg']);

$client->addTask('mysql',
                 'SELECT * FROM test.gearman_cookbook',
                 &$results['mysql']);

$image = file_get_contents("04_tree.jpg");
$client->addTask('resize',
                 $image,
                 &$results['resize']);

$client->setCompleteCallback("complete");
$client->runTasks();

printf("Jobs ran in %.6f seconds.\n", gettimeofday(true) - $begin);

print $results['dmesg']['handle'] . "\n";
print $results['dmesg']['data'] . "\n\n";

print $results['mysql']['handle'] . "\n";
var_dump(unserialize($results['mysql']['data']));
print "\n";

print $results['resize']['handle'] . "\n";
print strlen($image) . " -> " . strlen($results['resize']['data']) . "\n";
file_put_contents("04_tree_thumb.jpg", $results['resize']['data']);

?>
